<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="工单标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入工单标题" />
      </el-form-item>
      <el-form-item label="工单编号" prop="code">
        <el-input v-model="formData.code" placeholder="请输入工单编号" />
      </el-form-item>
      <el-form-item label="巡检人" prop="inspector">
        <el-input v-model="formData.inspector" placeholder="请输入巡检人" />
      </el-form-item>
      <el-form-item label="巡检日期" prop="inspectDate">
        <el-date-picker
          v-model="formData.inspectDate"
          type="date"
          value-format="x"
          placeholder="选择巡检日期"
        />
      </el-form-item>
      <el-form-item label="楼栋" prop="building">
        <el-input v-model="formData.building" placeholder="请输入楼栋" />
      </el-form-item>
      <el-form-item label="区域" prop="area">
        <el-input v-model="formData.area" placeholder="请输入区域" />
      </el-form-item>
      <el-form-item label="巡检项" prop="item">
        <el-input v-model="formData.item" placeholder="请输入巡检项" />
      </el-form-item>
      <el-form-item label="异常定位一" prop="location1">
        <el-input v-model="formData.location1" placeholder="请输入异常定位一" />
      </el-form-item>
      <el-form-item label="异常定位二" prop="location2">
        <el-input v-model="formData.location2" placeholder="请输入异常定位二" />
      </el-form-item>
      <el-form-item label="异常描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="异常描述补充" prop="descriptionExtra">
        <el-input
          type="textarea"
          v-model="formData.descriptionExtra"
          placeholder="请输入异常描述补充"
          rows="4"
        />
      </el-form-item>
      <el-form-item label="异常图片" prop="fileUrls">
        <UploadImgs v-model="formData.fileUrls" />
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="处理方法" prop="processMethod">
        <el-select v-model="formData.processMethod" placeholder="请选择处理方法">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="接收人" prop="receiver">
        <el-select v-model="formData.receiver" placeholder="请选择接收人">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="接收日期" prop="receiveDate">
        <el-date-picker
          v-model="formData.receiveDate"
          type="date"
          value-format="x"
          placeholder="选择接收日期"
        />
      </el-form-item>
      <el-form-item label="处理人" prop="processer">
        <el-select v-model="formData.processer" placeholder="请选择处理人">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理日期" prop="processDate">
        <el-date-picker
          v-model="formData.processDate"
          type="date"
          value-format="x"
          placeholder="选择处理日期"
        />
      </el-form-item>
      <el-form-item label="派单方式" prop="dispatchMethod">
        <el-select v-model="formData.dispatchMethod" placeholder="请选择派单方式">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理结果" prop="processResult">
        <el-select v-model="formData.processResult" placeholder="请选择处理结果">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理结果图片" prop="processFileUrls">
        <UploadImgs v-model="formData.processFileUrls" />
      </el-form-item>
      <el-form-item label="处理结果描述" prop="processDescription">
        <el-input
          v-model="formData.processDescription"
          type="textarea"
          placeholder="请输入处理结果描述"
          rows="4"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { WorkorderApi, WorkorderVO } from '@/api/pi/workorder'

/** PI 巡查巡检工单管理 表单 */
defineOptions({ name: 'WorkorderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  title: undefined,
  code: undefined,
  inspector: undefined,
  inspectDate: undefined,
  building: undefined,
  area: undefined,
  item: undefined,
  location1: undefined,
  location2: undefined,
  description: undefined,
  descriptionExtra: undefined,
  fileUrls: undefined,
  status: undefined,
  processMethod: undefined,
  receiver: undefined,
  receiveDate: undefined,
  processer: undefined,
  processDate: undefined,
  dispatchMethod: undefined,
  processResult: undefined,
  processFileUrls: undefined,
  processDescription: undefined
})
const formRules = reactive({})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WorkorderApi.getWorkorder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WorkorderVO
    if (formType.value === 'create') {
      await WorkorderApi.createWorkorder(data)
      message.success(t('common.createSuccess'))
    } else {
      await WorkorderApi.updateWorkorder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    title: undefined,
    code: undefined,
    inspector: undefined,
    inspectDate: undefined,
    building: undefined,
    area: undefined,
    item: undefined,
    location1: undefined,
    location2: undefined,
    description: undefined,
    descriptionExtra: undefined,
    fileUrls: undefined,
    status: undefined,
    processMethod: undefined,
    receiver: undefined,
    receiveDate: undefined,
    processer: undefined,
    processDate: undefined,
    dispatchMethod: undefined,
    processResult: undefined,
    processFileUrls: undefined,
    processDescription: undefined
  }
  formRef.value?.resetFields()
}
</script>
